package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.dto.CommentDTO;
import com.example.dto.QuestionDto;
import com.example.dto.ResultDTO;
import com.example.enums.CommentTypeEnum;
import com.example.model.LoginUser;
import com.example.model.Question;
import com.example.service.impl.CommentService;
import com.example.service.impl.Questionservice;
import entity.Result;
import entity.StatusCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Author by admin, Email xx@xx.com, Date on 2019/8/10.
 * PS: Not easy to write code, please indicate.
 */
@Controller
@Slf4j
public class QuestionController {
    //访问问题详细
    @Autowired
    private Questionservice questionservice;
    @Autowired
    private CommentService commentService;
    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/question/{id}")
    private String questionInfo(@PathVariable(name = "id")Integer id, Model model)
    {
        QuestionDto questionDto =questionservice.getInfoByid(id);
        List<QuestionDto> relatedQuestions = questionservice.selectRelated(questionDto);
        List<CommentDTO> comments = commentService.listByTargetId(id, CommentTypeEnum.QUESTION);
        questionservice.incview(id);
        model.addAttribute("question",questionDto);
        model.addAttribute("comments", comments);
        model.addAttribute("relatedQuestions",relatedQuestions);
        return "questionInfo";
    }

    @GetMapping("thumbUp/{id}")
    @ResponseBody
    public ResultDTO thumbUp(@PathVariable("id") String articleId){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if("anonymousUser" == authentication.getPrincipal()){
            return ResultDTO.errorOf(20000,"请登陆点赞！！！");
        }
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        //通过jwt 拿到userId
        Long userId = loginUser.getUser().getId();
        String accountId = loginUser.getUser().getAccountId();
        //查询文章点赞信息，根据用户id和文章id
        String key="thumbUp_article_"+articleId+"_"+accountId+"_"+userId;
        Object flag = redisTemplate.opsForValue().get(key);
        int upsum=0;
        if (flag==null){
            //没有用户点赞 文章。
            redisTemplate.opsForValue().set(key,1);
            questionservice.thumpUp(articleId,userId);
            Question byId = questionservice.getById(articleId);
            upsum=byId.getThumbup();
            return ResultDTO.okOf("点赞成功！",upsum);
        }else {
            redisTemplate.opsForValue().set(key,null);
            questionservice.thumpUp(articleId,userId);
            Question byId = questionservice.getById(articleId);
            upsum=byId.getThumbup();
            return ResultDTO.errorOf(2000,"点赞取消！",upsum);
        }
    }
    /*
    * 订阅文章
    * */
    @ResponseBody
    @PostMapping("/subscribe/{articleId}")
    public ResultDTO subscribe(@PathVariable String articleId){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        //通过jwt 拿到userId
        String userid = String.valueOf(loginUser.getUser().getId());
        Boolean flag =  questionservice.subscribe(articleId,userid);
        if (flag==true){
            return ResultDTO.okOf("关注成功！");
        }else {
            return ResultDTO.errorOf(2001,"取消关注！");
        }
    }
    @ResponseBody
    @GetMapping("/queryAllQuestion")
    public Result queryAllQuestion(){
        List<Question> list = questionservice.list(new QueryWrapper<>());
        return new Result(true, StatusCode.OK,"查询成功",list);
    }
}
